package org.lookouster.nextAgadir.pointageBureau.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/*
 * Identification.java
 *
 * Created on 25 avril 2008, 02:25
 */
import javax.swing.WindowConstants;

import org.lookouster.nextAgadir.pointageBureau.bases.Compte;
import org.lookouster.nextAgadir.pointageBureau.outils.BDFunctions;
import org.lookouster.nextAgadir.pointageBureau.outils.MyBlowFish;
import org.lookouster.nextAgadir.pointageBureau.outils.functions;



/**
 *
 * @author  Lookouster
 */
public class Identification extends javax.swing.JDialog {
	
	 // Variables declaration - do not modify                     
    private javax.swing.JButton btnValider;
    private javax.swing.JButton btnAnnuler;
    private javax.swing.JLabel lblTitle,lblNotif;
    private javax.swing.JLabel lblPass,lblLogin;
    private javax.swing.JPasswordField password;
    private JTextField login;
    // End of variables declaration    
    
    MainUI main;
    
    /** Creates new form Identification */
    public Identification() {
        super();
        
        initComponents();
        setLocationRelativeTo(null);
        
     JDialog.setDefaultLookAndFeelDecorated(true);
    }
    
    public Identification(JFrame parent)
    {
    	super(parent);
    	initComponents();
    }
    
                          
    private void initComponents() {

        password = new javax.swing.JPasswordField();
        login = new javax.swing.JTextField();
        lblTitle = new javax.swing.JLabel();
        lblNotif=new JLabel(UICommons.fIdNotif);
        lblPass = new javax.swing.JLabel();
        lblLogin = new javax.swing.JLabel();
        btnValider = new javax.swing.JButton();
        btnAnnuler = new javax.swing.JButton();

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        
        
        btnAnnuler.addActionListener(new ActionListener()
        {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
        	
        });
        
        btnValider.addActionListener(new ActionListener()
        {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					identify();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
        	
        });
        
        
        

        lblTitle.setText(UICommons.fIdTitle);
       
        lblNotif.setVisible(false);
        lblPass.setText(UICommons.fIdPass);
        lblLogin.setText(UICommons.fIdUser);
        

        btnValider.setText(UICommons.btnValid);
        
        lblNotif.setForeground(Color.red);
        
        btnAnnuler.setText(UICommons.btnCancel);
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(lblNotif, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(btnValider)
                        .addGap(41, 41, 41)
                        .addComponent(btnAnnuler))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(lblTitle))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPass)
                            .addComponent(lblLogin))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(login,javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(password))))
                .addContainerGap(118, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblTitle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblLogin)
                        .addGap(18, 18, 18)
                        .addComponent(lblPass)))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAnnuler)
                    .addComponent(btnValider))
                .addGap(18, 18, 18)
                .addComponent(lblNotif)
                .addContainerGap(31, Short.MAX_VALUE))
       
        );

        pack();
    }                        
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Identification dialog = new Identification();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public void identify() throws Exception
    {
    	String uname;
		String pass;
    	uname=login.getText();
    	pass=String.valueOf(password.getPassword());
    	MyBlowFish bf = new MyBlowFish();
    	bf.setSecretKey(UICommons.DEF_KEY);
    	
    	byte[] cryptPass = bf.crypt(pass); 
    	final Compte user=BDFunctions.logIn(uname, 
    			String.valueOf(new BigInteger(cryptPass)));
    	if(user==null)
    	{
    		lblNotif.setVisible(true);
    		login.setText("");
    		password.setText("");
    	}
    	else
    	{
    		java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                	if (main == null) {
                		main = new MainUI(user);
                    }
                	main.setUser(user);
                    
                    
                    main.addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    main.setSize(new Dimension(1080,700));
                    main.setLocation(100, 50);
                    main.setVisible(true);
                }
            });
    		this.setVisible(false);
    	}
    	    		
    }
    
                 
    
}

